<?php


namespace app\admin\controller;


use app\admin\validate\IDMustBePositiveInt;
use app\admin\validate\RoleValidate;
use app\admin\validate\UserValidate;
use app\service\Jurisdiction;
use app\service\model\AdminRoleModel;
use app\service\Y;
use think\facade\Cache;
use think\Request;

class Role extends BaseAdmin
{

    /**
     * 角色列表数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(Request $request): \think\response\Json
    {
        $where = [];
        $data = AdminRoleModel::where($where)->paginate([
            'list_rows'=> input('param.limit', 10),
            'query' => input('param.')
        ])->toArray();
        return Y::table($data['data'], $data['total']);
    }

    /**
     * 保存角色信息
     * @param Request $request
     * @param RoleValidate $role_validate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, RoleValidate $role_validate): \think\response\Json
    {
        $id = input('param.id', 0);
        if ($request->isPost()) {
            $role_validate->goCheck();
            $param = $role_validate->getDataByRule();
            return AdminRoleModel::saveRole($param, $id);
        }

        $data = [];
        if ($id > 0) {
            $data = AdminRoleModel::find($id);
        }
        return Y::success($data, '角色数据');
    }

    /**
     * 删除数据
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        return $this->deleteDate((new AdminRoleModel()), $id);
    }

    /**
     * 设置权限
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function set_auth(IDMustBePositiveInt $IDMustBePositiveInt) {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $role = AdminRoleModel::find($id);
        if (request()->isPost()) {
            try {
                if (!$role) {
                    throw new \Exception('角色不存在');
                }
                if (!input('?param.auth_ids')) {
                    throw new \Exception('请分配权限');
                }
                $authIds = input('param.auth_ids/a');
                $data = ['authoritys' => implode(',', $authIds)];
                $role->save($data);
                // 角色权限有修改的时候，清空缓存
                Cache::tag('admin_cache')->clear();

                return Y::success([], '保存成功');
            } catch (\Exception $e) {
                return Y::error($e->getMessage());
            }
        } else {
            return view('', compact('role'));
        }
    }

    /**
     * 获取节点树信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_tree(): \think\response\Json
    {
        $id = input('param.id', 0);
        $auths = Jurisdiction::getAllAuth($id);

        // 获取节点配置信息
        return json(['code' => 0, 'tree' => $auths]);
    }

    /**
     * 获取XmSelect组件所需的数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function xm_select_data(): \think\response\Json
    {
        $admin_id = input('param.admin_id', 0);
        $data = AdminRoleModel::xmSelectDate($admin_id);
        return Y::table($data);
    }
}